<template>
  <div>
    <div class="project" v-if="!projectAddShow && !followAddShow">
      <!-- 筛选按钮 -->
      <div class="search-button">
        <el-button
          v-if="route.name === 'ReadyProject'"
          v-hasPermi="['system:readyProject:query']"
          class="btn search"
          @click="getProjectList"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button
          v-if="route.name === 'ReadyProjectCheck'"
          v-hasPermi="['system:readyProjectCheck:query']"
          class="btn search"
          @click="getProjectList"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button class="btn add" @click="handleRefresh"
          ><el-icon class="icon" color="#fff"> <Refresh /> </el-icon
          >重置</el-button
        >
        <el-popover :width="170" trigger="click" placement="bottom">
          <template #reference>
            <el-button class="btn search" style="width: 100px"
              ><el-icon class="icon" color="#fff"> <Setting /> </el-icon
              >显示列</el-button
            >
          </template>
          <el-checkbox-group v-model="checkList" @change="handleCheckChange">
            <div>
              <el-checkbox label="purchaseContractCode">合同编号</el-checkbox>
            </div>
            <div>
              <el-checkbox label="purchaseContractName">合同名称</el-checkbox>
            </div>
            <div>
              <el-checkbox label="applicantDate">申请日期</el-checkbox>
            </div>
            <div>
              <el-checkbox label="contractTypeName">类型</el-checkbox>
            </div>
            <div>
              <el-checkbox label="contractCheckStatusName">审核状态</el-checkbox>
            </div>
            <div>
              <el-checkbox label="applicantName">经办人</el-checkbox>
            </div>
            <div>
              <el-checkbox label="deptName">经办部门</el-checkbox>
            </div>
            <div>
              <el-checkbox label="distributorName">供应商</el-checkbox>
            </div>

            <div>
              <el-checkbox label="purchaseAmountSum">采购总金额(元)</el-checkbox>
            </div>
            <div>
              <el-checkbox label="paymentAmount">已付金额(元)</el-checkbox>
            </div>
            <div>
              <el-checkbox label="totalMoney">到票金额(元)</el-checkbox>
            </div>
            <div>
              <el-checkbox label="nextNodeName">下步流程</el-checkbox>
            </div>
            <div>
              <el-checkbox label="storageStatus">入库状态</el-checkbox>
            </div>
          </el-checkbox-group>
        </el-popover>
      </div>
      <!-- 筛选条件 -->
      <div class="search">
        <div class="search-item">
          <el-input
            clearable
            v-model="params.projectCode"
            class="search-input"
            placeholder="请输入项目编号"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model="params.projectName"
            class="search-input"
            placeholder="请输入项目名称"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model="params.pm"
            class="search-input"
            placeholder="请输入项目经理"
          ></el-input>
        </div>
        <div class="search-item">
          <el-select
            v-model="params.projectStatus"
            placeholder="请选择项目状态"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in statusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
        <div class="search-item">
          <el-select
            v-model="params.projectCategory"
            placeholder="请选择项目种类"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in kindList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
        <div class="search-item">
          <el-select
            v-model="params.projectType"
            placeholder="请选择项目类型"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in typeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
        <!-- <div class="search-item">
          <el-select
            v-model="params.arrivalStatus"
            placeholder="请选择到货状态"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in []"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
         -->
        <div class="search-item">
          <el-select
            v-model="params.practiceStatus"
            placeholder="请选择实施状态"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in putList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
      </div>

      <!-- 表格数据 -->
      <el-table
        v-loading="loading"
        :data="projectList"
        border
        height="64vh"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <!-- 操作模式 -->
        <el-table-column
          label="操作"
          :show-overflow-tooltip="true"
          :width="route.name === 'ReadyProject' ? '100px' : '100px'"
        >
          <template #default="{ row }">
            <div class="table-operate">
              <el-icon
                v-if="route.name === 'ReadyProject'"
                v-hasPermi="['system:readyProject:detail']"
                title="查看详情"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('detail', row)"
              >
                <Document />
              </el-icon>
              <el-icon
                v-if="route.name === 'ReadyProjectCheck'"
                v-hasPermi="['system:ReadyProjectCheck:detail']"
                title="查看详情"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('detail', row)"
              >
                <Document />
              </el-icon>
              <el-icon
                v-if="route.name === 'ReadyProject' && row.practiceStatus === 0"
                v-hasPermi="['system:readyProject:firing']"
                title="启动"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('start', row)"
              >
                <DocumentChecked />
              </el-icon>
            </div> </template
        ></el-table-column>
        <el-table-column
          type="index"
          label="序号"
          :index="indexMethod"
          width="70"
          align="center"
        />
        <el-table-column
          label="项目编号"
          prop="projectCode"
          :show-overflow-tooltip="true"
          width="180"
        />
        <el-table-column
          label="项目名称"
          prop="projectName"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="实施状态"
          prop="practiceStatusName"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="项目状态"
          prop="projectStatusName"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="项目种类"
          prop="projectCategoryName"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="项目类型"
          prop="projectTypeName"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="创建部门"
          prop="deptName"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="创建人"
          prop="createUserName"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="业务人员"
          prop="businessName"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="同步时间"
          prop="synchronizationTime"
          :show-overflow-tooltip="true"
          width="170"
        />
        <el-table-column
          label="期望安装完成时间"
          prop="installationCompletionTime"
          :show-overflow-tooltip="true"
          width="180"
          >
          <template #default="{ row }">
            <span>{{
              row.installationCompletionTime ? row.installationCompletionTime : "--"
            }}</span>
          </template>
        </el-table-column>
        
        <el-table-column
          label="项目经理"
          prop="pm"
          :show-overflow-tooltip="true"
          width="100"
          >
          <template #default="{ row }">
            <span>{{
              row.pm ? row.pm : "--"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="项目开始时间"
          prop="projectStartTime"
          :show-overflow-tooltip="true"
          width="160"
          >
          <template #default="{ row }">
            <span>{{
              row.projectStartTime ? row.projectStartTime : "--"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="项目结束时间"
          prop="projectEndTime"
          :show-overflow-tooltip="true"
          width="160"
          >
          <template #default="{ row }">
            <span>{{
              row.projectEndTime ? row.projectEndTime : "--"
            }}</span>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        class="pagination"
        :total="total"
        v-model:page="params.pageNum"
        v-model:limit="params.pageSize"
        @pagination="getProjectList"
      />
    </div>
    <ProjectAdd
      v-if="projectAddShow"
      ref="projectAddRef"
      v-model:projectAddShow="projectAddShow"
      @updateFollowAddShow="followAddShow = false"
      @refreshList="getProjectList"
      :pageType="pageType"
    />
    <FollowAdd
      ref="followAddRef"
      v-model:followAddShow="followAddShow"
      @refreshList="getProjectList"
      :pageType="pageType"
      v-if="followAddShow"
    />
  </div>
</template>

<script setup>
import FollowAdd from "./followAdd.vue";
import ProjectAdd from "./projectAdd.vue";
import { getProjectListApi, deleteProjectApi } from "@/api/sales/project";
import { getReadyProjectListApi } from "@/api/operation/project";
import { getDictsApi } from "@/api/dict";
import { addTableHeaderApi, getTableHeaderApi } from "@/api/pub";
import { ElMessage, ElMessageBox } from "element-plus";
// 路由
const route = useRoute();
// 搜索参数
const params = reactive({
  arrivalStatus: null, //到货状态
  checkStatus: null, // 审核状态
  includeContract: null, //是否签合同
  practiceStatus: null, //实施状态
  projectCategory: null, // 项目种类
  projectCode: "", // 项目编号
  projectName: "", // 项目名称
  projectStatus: null, // 项目状态
  projectType: null, // 	项目类型
  pageNum: 1,
  pageSize: 50,
});
// 表格加载效果
const loading = ref(false);
// 项目新增组件显隐
const projectAddShow = ref(false);
// 跟进新增组件显隐
const followAddShow = ref(false);
// 项目列表
const projectList = ref([]);
// 总数
const total = ref(0);
// 项目新增组件引用
const projectAddRef = ref(null);
// 跟进组件引用
const followAddRef = ref(null);
// 操作类型
const pageType = ref("");
// 审核状态列表
const checkOptions = ref([]);
// 是否签合同列表
const contractList = ref([]);
// 项目种类列表
const kindList = ref([]);
// 项目状态列表
const statusList = ref([]);
// 项目类型列表
const typeList = ref([]);
//实施状态
const putList = ref([]);

const checkList = ref([
  "projectCode",
  "projectName",
  "practiceStatusName",
  "projectStatusName",
  "projectCategoryName",
  "projectTypeName",
  "deptName",
  "createUserName",
  "businessName",
  "synchronizationTime",
  "installationCompletionTime",
  "pm",
  "projectStartTime",
  "projectEndTime",
]);

/**
 * @description: 复选框选中
 */
 const handleCheckChange = async (val) => {
  var str = val.join(",");
  const result = await addTableHeaderApi({
    moduleType: 19,
    titleField: str,
  });
};

/**
 * @description: 获取表头
 */
const getTableHeader = async () => {
  const result = await getTableHeaderApi({
    moduleType: 19,
  });
  if (result.code == 200) {
    var arr = result.data.titleField.split(",");
    checkOptions.value = arr;
  }
};

/**
 * @description: 重置
 */
 const handleRefresh = () => {
  Object.assign(params, {
    arrivalStatus: null, //到货状态
  checkStatus: null, // 审核状态
  includeContract: null, //是否签合同
  practiceStatus: null, //实施状态
  projectCategory: null, // 项目种类
  projectCode: "", // 项目编号
  projectName: "", // 项目名称
  projectStatus: null, // 项目状态
  projectType: null, // 	项目类型
  pm:'',
    pageNum: 1,
    pageSize: 50,
  });
  getProjectList()
};

/**
 * @description: 获取项目列表
 */
const getProjectList = async () => {
  try {
    loading.value = true;
    let { pageNum, pageSize } = params;
    const res = await getReadyProjectListApi({ pageNum, pageSize }, params);
    projectList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  } catch {
    loading.value = false;
  }
};

/**
 * @description: 表格操作
 * @param {String} type 类型
 * @param {Object} row  当前行数据
 */
const handleOperate = async (type, row) => {
  switch (type) {
    case "edit":
      pageType.value = type;
      projectAddShow.value = true;
      nextTick(() => {
        projectAddRef.value.handleDetailInfo(row.projectId);
      });
      break;
    case "check":
      pageType.value = type;
      projectAddShow.value = true;
      followAddShow.value = true;
      nextTick(() => {
        projectAddRef.value.handleDetailInfo(row.projectId);
        followAddRef.value.handleDetailInfo(row.projectId);
      });
      break;
    case "detail":
      pageType.value = type;
      projectAddShow.value = true;
      followAddShow.value = true;
      nextTick(() => {
        projectAddRef.value.handleDetailInfo(row.projectId);
        followAddRef.value.handleDetailInfo(row.projectId);
      });
      break;
    case "start":
      pageType.value = type;
      followAddShow.value = true;
      nextTick(() => {
        followAddRef.value.handleDetailInfo(row.projectId);
      });
      break;
    case "delete":
      ElMessageBox.confirm("此操作将删除该项目，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
        type: "warning",
      })
        .then(async () => {
          const res = await deleteProjectApi(row.projectId);
          if (res.code === 200) {
            ElMessage.success(res.msg);
            getProjectList();
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
      break;

    default:
      break;
  }
};

/**
 * @description: 获取字典
 */
async function getDicts() {
  await getDictsApi("erp_project_check_status").then((res) => {
    // 审核状态
    if (res.code === 200) {
      checkList.value = res.data;
    }
  });

  await getDictsApi("erp_include_contract").then((res) => {
    // 是否签合同
    if (res.code === 200) {
      contractList.value = res.data;
    }
  });
  await getDictsApi("erp_project_category").then((res) => {
    // 项目种类
    if (res.code === 200) {
      kindList.value = res.data;
    }
  });
  await getDictsApi("erp_practice_status").then((res) => {
    // 实施状态
    if (res.code === 200) {
      putList.value = res.data;
    }
  });
  await getDictsApi("erp_main_project_status").then((res) => {
    // 项目状态
    if (res.code === 200) {
      statusList.value = res.data;
      // res.data.forEach(item => {
      //   if(item.value >2) {
      //     statusList.value.push(item)
      //   }
      // })
      
    }
  });
  await getDictsApi("erp_project_type").then((res) => {
    // 项目类型
    if (res.code === 200) {
      typeList.value = res.data;
    }
  });
}

/**
 * @description: 序号
 * @param {Number} index 索引
 */
function indexMethod(index) {
  const currentPage = params.pageNum;
  const pageSize = params.pageSize;
  const truelndex = (currentPage - 1) * pageSize + index + 1;
  return truelndex;
}

onMounted(() => {
  getTableHeader();
  getProjectList();
  getDicts();
});
</script>
<style>
@import "@/assets/styles/confirmBtn.css";
</style>
<style lang="scss" scoped>
.project {
  padding: 20px;
  width: 100%;
  height: 100%;
  .search-item {
    // margin-bottom: 20px;
  }
  .pagination {
    margin-top: 20px;
  }
}
</style>
